package route

import (
	"ginApi/controller"
	"ginApi/service"
	"ginApi/web/middleware"
	"github.com/gin-gonic/gin"
)

// NewRouter 路由配置
func NewRouter() *gin.Engine {
	r := gin.Default()

	r.Use(middleware.Cors())

	tokenService := service.NewTokenService()
	userService := service.NewUserService()
	userApi := controller.UserController{ServiceUser: userService, ServiceToken: tokenService}

	v1 := r.Group("/api/v1")
	{
		v1.POST("/user/register", userApi.Register)
		v1.POST("/user/login", userApi.Login)

		// 需要登录保护的
		auth := v1.Group("/")
		auth.Use(middleware.AccessRequired(tokenService))
		{
			auth.POST("user/logout", userApi.Logout)
		}
	}

	return r
}
